/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.PlpfCost;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.PlpfCostRepository;
import me.zhengjie.business.service.PlpfCostService;
import me.zhengjie.business.service.dto.PlpfCostDto;
import me.zhengjie.business.service.dto.PlpfCostQueryCriteria;
import me.zhengjie.business.service.mapstruct.PlpfCostMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2023-07-04
**/
@Service
@RequiredArgsConstructor
public class PlpfCostServiceImpl implements PlpfCostService {

    private final PlpfCostRepository plpfCostRepository;
    private final PlpfCostMapper plpfCostMapper;

    @Override
    public Map<String,Object> queryAll(PlpfCostQueryCriteria criteria, Pageable pageable){
        Page<PlpfCost> page = plpfCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(plpfCostMapper::toDto));
    }

    @Override
    public List<PlpfCostDto> queryAll(PlpfCostQueryCriteria criteria){
        return plpfCostMapper.toDto(plpfCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PlpfCostDto findById(Long bisPlpfCostId) {
        PlpfCost plpfCost = plpfCostRepository.findById(bisPlpfCostId).orElseGet(PlpfCost::new);
        ValidationUtil.isNull(plpfCost.getBisPlpfCostId(),"PlpfCost","bisPlpfCostId",bisPlpfCostId);
        return plpfCostMapper.toDto(plpfCost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlpfCostDto create(PlpfCost resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setBisPlpfCostId(snowflake.nextId()); 
        return plpfCostMapper.toDto(plpfCostRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PlpfCost resources) {
        PlpfCost plpfCost = plpfCostRepository.findById(resources.getBisPlpfCostId()).orElseGet(PlpfCost::new);
        ValidationUtil.isNull( plpfCost.getBisPlpfCostId(),"PlpfCost","id",resources.getBisPlpfCostId());
        plpfCost.copy(resources);
        plpfCostRepository.save(plpfCost);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long bisPlpfCostId : ids) {
            plpfCostRepository.deleteById(bisPlpfCostId);
        }
    }

    @Override
    public void download(List<PlpfCostDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PlpfCostDto plpfCost : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}